package com.zhaotianhao.task;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.zhaotianhao.pojo.Order;
import com.zhaotianhao.service.OrderItemService;
import com.zhaotianhao.service.OrderService;
import com.zhaotianhao.util.AlibabapayGateWayUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 掉单，未支付单的处理
 * @Author Z-7
 * @Date 2022/3/4
 */
@Lazy(false)
@Component
@EnableScheduling
public class OrderTask {
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private OrderItemService orderItemService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private KafkaTemplate kafkaTemplate;
    /**
     * 掉单处理
     * 检查支付宝中已经到账，但是本系统当中状态未修改的订单
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void processLostOrder() throws AlipayApiException {
        // 查询5分钟前没有支付的订单
        List<Order> list = orderService.listUnPay(5);
        Map<String, String> map = new HashMap<>();
        // 查询支付宝上订单是否支付
        for (Order order : list) {
            map.put("out_trade_no",order.getId());
            AlipayTradeQueryResponse alipayTradeQueryResponse = AlibabapayGateWayUtils.queryOrder(map);
            if(("TRADE_SUCCESS").equals(alipayTradeQueryResponse.getTradeStatus())){
                // 订单已经支付
                orderService.updateOrderStatus(order.getId(),1);
                System.out.println("漏单："+order.getId()+"处理完成");
            }else{
                // 可以催单

            }
        }
    }

    /**
     * 取消未支付订单
     * @throws AlipayApiException
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void processCansoleOrder() throws AlipayApiException {
        // 查询10分钟前没有支付的订单
        List<Order> list = orderService.listUnPay(10);
        Map<String, String> map = new HashMap<>();
        // 查询支付宝上订单是否支付
        for (Order order : list) {
            map.put("out_trade_no",order.getId());
            AlipayTradeQueryResponse alipayTradeQueryResponse = AlibabapayGateWayUtils.queryOrder(map);
            if(("TRADE_SUCCESS").equals(alipayTradeQueryResponse.getTradeStatus())){
                // 订单已经支付
                orderService.updateOrderStatus(order.getId(),1);
                System.out.println("漏单："+order.getId()+"处理完成");
            }else{
                // 取消订单释放库存
                orderService.deleteById(order.getId());
                orderItemService.deleteByOrderId(order.getId());
                System.out.println("超时未支付，取消："+order.getId());
            }
        }
    }

   /* *//**
     * 消息补偿
     * 重新发送一遍
     *//*
    @Scheduled(cron = "0 * * * * ?")
    public void reSendMsg(){
        List<Order> orderList = redisTemplate.opsForHash().values("order");
        if(orderList == null){
            return;
        }
        // 重新发送给消费者完成消费
        for (Order order : orderList) {
            kafkaTemplate.send("hgShop","order", JSON.toJSONString(order));
        }
    }*/
}
